/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.purchase.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.letmeshare.stockkeeper.stock.entities.Item;
import org.letmeshare.stockkeeper.validator.ValidationException;
import org.letmeshare.stockkeeper.validator.Validator;

/**
 *
 * @author ibisuser1
 */
@Entity
public class PurchaseEntry implements Serializable, Validator {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    @ManyToOne
    private Item item;
    private double qty;
    private double purchaseRate;
    private double exclusiveTax;//if any

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public double getQty() {
        return qty;
    }

    public void setQty(double qty) {
        this.qty = qty;
    }

    public double getPurchaseRate() {
        return purchaseRate;
    }

    public void setPurchaseRate(double purchaseRate) {
        this.purchaseRate = purchaseRate;
    }

    public double getExclusiveTax() {
        return exclusiveTax;
    }

    public void setExclusiveTax(double exclusiveTax) {
        this.exclusiveTax = exclusiveTax;
    }

    public double getAmount() {
        return Math.round(qty * (this.purchaseRate + this.purchaseRate * this.exclusiveTax/100) * 100.0)/100.0;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PurchaseEntry)) {
            return false;
        }
        PurchaseEntry other = (PurchaseEntry) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.letmeshare.stockkeeper.entities.purchase.PurchaseEntry[ id=" + id + " ]";
    }

    public void validate() throws ValidationException {
        if (item == null) {
            throw new ValidationException("Item required");
        }
        item.validate();
        if (qty <= 0) {
            throw new ValidationException("Invalid quantity");
        }
        if (purchaseRate < 0) {
            throw new ValidationException("Invalid purchase rate");
        }
        if (exclusiveTax<0) {
            throw new ValidationException("Invalid tax");
        }
    }
    
}
